home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / fs / utility.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  5KB  |  162 lines

  1. /* This file contains a few general purpose utility routines.
  2.  *
  3.  * The entry points into this file are
  4.  *   clock_time:  ask the clock task for the real time
  5.  *   copy:      copy a block of data
  6.  *   fetch_name:  go get a path name from user space
  7.  *   no_sys:      reject a system call that FS does not handle
  8.  *   panic:       something awful has occurred;  MINIX cannot continue
  9.  */
  10.  
  11. #include "fs.h"
  12. #include <minix/com.h>
  13. #include <minix/boot.h>
  14. #include "buf.h"
  15. #include "file.h"
  16. #include "fproc.h"
  17. #include "inode.h"
  18. #include "param.h"
  19. #include "super.h"
  20.  
  21. PRIVATE int panicking;        /* inhibits recursive panics during sync */
  22. PRIVATE message clock_mess;
  23.  
  24. /*===========================================================================*
  25.  *                clock_time                     *
  26.  *===========================================================================*/
  27. PUBLIC time_t clock_time()
  28. {
  29. /* This routine returns the time in seconds since 1.1.1970.  MINIX is an
  30.  * astrophysically naive system that assumes the earth rotates at a constant
  31.  * rate and that such things as leap seconds do not exist.
  32.  */
  33.  
  34.   register int k;
  35.   register struct super_block *sp;
  36.  
  37.   clock_mess.m_type = GET_TIME;
  38.   if ( (k = sendrec(CLOCK, &clock_mess)) != OK) panic("clock_time err", k);
  39.  
  40.   /* Since we now have the time, update the super block.  It is almost free. */
  41.   sp = get_super(ROOT_DEV);
  42.   if (sp) {
  43.     sp->s_time = clock_mess.NEW_TIME;    /* update super block time */
  44.     if (sp->s_rd_only == FALSE) sp->s_dirt = DIRTY;
  45.   }
  46.  
  47.   return (time_t) clock_mess.NEW_TIME;
  48. }
  49.  
  50.  
  51. /*===========================================================================*
  52.  *                copy                         *
  53.  *===========================================================================*/
  54. PUBLIC void copy(dest, source, bytes)
  55. char *dest;            /* destination pointer */
  56. char *source;            /* source pointer */
  57. int bytes;            /* how much data to move */
  58. {
  59. /* Copy a byte string of length 'bytes' from 'source' to 'dest'.
  60.  * If all three parameters are exactly divisible by the integer size, copy them
  61.  * an integer at a time.  Otherwise copy character-by-character.
  62.  */
  63.  
  64.   int src, dst;
  65.  
  66.   if (bytes <= 0) return;    /* makes test-at-the-end possible */
  67.   src = (int) source;        /* only low-order bits needed */    
  68.   dst = (int) dest;        /* only low-order bits needed */
  69.  
  70.   if (bytes % sizeof(int) == 0 && src % sizeof(int) == 0 &&
  71.                         dst % sizeof(int) == 0) {
  72.     /* Copy the string an integer at a time. */
  73.     register int n = bytes/sizeof(int);
  74.     register int *dpi = (int *) dest;
  75.     register int *spi = (int *) source;
  76.  
  77.     do { *dpi++ = *spi++; } while (--n);
  78.  
  79.   } else {
  80.  
  81.     /* Copy the string character-by-character. */
  82.     register int n = bytes;
  83.     register char *dpc = (char *) dest;
  84.     register char *spc = (char *) source;
  85.  
  86.     do { *dpc++ = *spc++; } while (--n);
  87.  
  88.   }
  89. }
  90.  
  91.  
  92. /*===========================================================================*
  93.  *                fetch_name                     *
  94.  *===========================================================================*/
  95. PUBLIC int fetch_name(path, len, flag)
  96. char *path;            /* pointer to the path in user space */
  97. int len;            /* path length, including 0 byte */
  98. int flag;            /* M3 means path may be in message */
  99. {
  100. /* Go get path and put it in 'user_path'.
  101.  * If 'flag' = M3 and 'len' <= M3_STRING, the path is present in 'message'.
  102.  * If it is not, go copy it from user space.
  103.  */
  104.  
  105.   register char *rpu, *rpm;
  106.   vir_bytes vpath;
  107.  
  108.   if (len <= 0) {
  109.     err_code = EINVAL;
  110.     return(ERROR);
  111.   }
  112.   if (flag == M3 && len <= M3_STRING) {
  113.     /* Just copy the path from the message to 'user_path'. */
  114.     rpu = &user_path[0];
  115.     rpm = pathname;        /* contained in input message */
  116.     do { *rpu++ = *rpm++; } while (--len);
  117.     return(OK);
  118.   }
  119.  
  120.   /* String is not contained in the message.  Go get it from user space. */
  121.   if (len > PATH_MAX) {
  122.     err_code = ENAMETOOLONG;
  123.     return(ERROR);
  124.   }
  125.   vpath = (vir_bytes) path;
  126.   err_code = rw_user(D, who, vpath, (vir_bytes) len, user_path, FROM_USER);
  127.   return(err_code);
  128. }
  129.  
  130.  
  131. /*===========================================================================*
  132.  *                no_sys                         *
  133.  *===========================================================================*/
  134. PUBLIC int no_sys()
  135. {
  136. /* Somebody has used an illegal system call number */
  137.  
  138.   return(EINVAL);
  139. }
  140.  
  141.  
  142. /*===========================================================================*
  143.  *                panic                         *
  144.  *===========================================================================*/
  145. PUBLIC void panic(format, num)
  146. char *format;            /* format string */
  147. int num;            /* number to go with format string */
  148. {
  149. /* Something awful has happened.  Panics are caused when an internal
  150.  * inconsistency is detected, e.g., a programming error or illegal value of a
  151.  * defined constant.
  152.  */
  153.  
  154.   if (panicking) return;    /* do not panic during a sync */
  155.   panicking = TRUE;        /* prevent another panic during the sync */
  156.   printf("File system panic: %s ", format);
  157.   if (num != NO_NUM) printf("%d",num); 
  158.   printf("\n");
  159.   (void) do_sync();            /* flush everything to the disk */
  160.   sys_abort();
  161. }
  162.